package cn.honglian.aop;


import cn.honglian.system.domain.Log;
import cn.honglian.system.mapper.LogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.util.Date;

@Aspect
@Component
public class LogAop {

    @Autowired
    private LogMapper logMapper;

    @Pointcut("@annotation(cn.honglian.aop.Log_Aop)")
    public void pointcut(){}


    @Around("pointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        String annoName = method.getAnnotation(Log_Aop.class).name();

        InetAddress localHost = Inet4Address.getLocalHost();
        String ipAddress = localHost.getHostAddress();

        logMapper.add(new Log(null, "本地操作", methodName, annoName, ipAddress, new Date()));
        Object proceed = pjp.proceed();
        return proceed;

    }

}
